package org.marsdonne.sync;

import com.fasterxml.jackson.databind.JsonNode;

import java.util.Collection;
import java.util.Date;

public interface Syncable {
    void sync(Event event);

    default void sync(Action action, String domain, Date dateSynced, JsonNode content) {
        this.sync(new Event(action, domain, dateSynced, content));
    }

    default void sync(Action action, String domain, JsonNode content) {
        this.sync(new Event(action, domain, new Date(), content));
    }

    default void created(String domain, JsonNode content) {
        this.sync(Action.CREATED, domain, content);
    }

    default void changed(String domain, JsonNode content) {
        this.sync(Action.CHANGED, domain, content);
    }

    default void deleted(String domain, JsonNode content) {
        this.sync(Action.DELETED, domain, content);
    }

    default void createdAll(final String domain, Collection<JsonNode> contents) {
        Date now = new Date();
        for (JsonNode content : contents) {
            this.sync(Action.CREATED, domain, now, content);
        }
    }

    default void changedAll(final String domain, Collection<JsonNode> contents) {
        Date now = new Date();
        for (JsonNode content : contents) {
            this.sync(Action.CHANGED, domain, now, content);
        }
    }

    default void deletedAll(final String domain, Collection<JsonNode> contents) {
        Date now = new Date();
        for (JsonNode content : contents) {
            this.sync(Action.DELETED, domain, now, content);
        }
    }
}
